app.controller('pv6', function ($scope, $rootScope, $http, $interval, $compile, $state) {
    //pv首页
    $scope.pv1_0 = function () {
        $state.go(pv1_0.stateName);
    }

    //修改案例状态
    $scope.changeStatus = function () {
        if(!$scope.id){
            return;
        }
        var remark = $(".remark").val();
        if(remark.length == 0){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "备注不能为空"));
            return;
        }
        if(remark.length > 100){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "备注字数不能超过100个字"));
            return;
        }
        
        if(remark.length == 0 && $scope.imageUrls.length == 0){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "备注或拍照不能为空"));
            return;
        }

        var postData = {
            "caseId": $scope.id,
            "optionItem": $scope.item.optionItem,
            "urls": JSON.stringify($scope.imageUrls)
        }
        postData.remark = $(".remark").val();
        var isfond = localStorage.getItem("pv-isfond");
        postData.isFond = isfond;

        console.log($(".remark"))
        console.log(postData);

        //alert(JSON.stringify(postData));
        postHttp($http, 'pvCase/updateCaseStatus', postData, function (res) {
            if (res.code === 0 && res.msg == "success") {
                // $rootScope.tipPopups.show(($scope.tipMessage = '成功'));
                $scope.pv1_0();
            } else {
                $rootScope.tipPopups.show(($rootScope.tipMessage = res.msg));
            }
        });
    }

    //拍照
    $scope.takePhoto = function () {
        var scanType = "pv2UploadImageResult";
        if(checkPhone('android')){
            AndroidMethod.uploadImage(getToken(),scanType);
        }else if(checkPhone("iphone")){
//             context.uploadImage(getToken(),scanType);
          window.webkit.messageHandlers.uploadImage.postMessage({"token": getToken(),"scanType":scanType});
        }else{
			hm.uploadImage(getToken(),scanType);
			
		}
    }

    //存入数组图片路径
    $scope.photoResult = function (url) {
        /*if(typeof(url)=='string'){
            $scope.imageUrls.push(JSON.parse(url).imageurl);
        }else{
            $scope.imageUrls.push(url.imageurl);
        }
        $scope.$applyAsync();*/


        if (navigator.onLine){ //正常工作
            if(typeof(url)=='string'){
               var jsonData = JSON.parse(url).imageurl;
               for (var i = 0; i < jsonData.length; i++) {
                   var counter = jsonData[i].url;
               	 $scope.imageUrls.push(counter);
               }
            }else{
                $scope.imageUrls.push(url.imageurl);
            }
            $scope.$applyAsync();
        }else { //执行离线状态时的任务
            $rootScope.tipPopups.show(($scope.tipMessage = "当前网络不佳，稍后再试"));
            return;
        }
    }

    //删除图片
    $scope.removeImg = function (index) {
        $scope.imageUrls.splice(index,1);
        $scope.$applyAsync();
    }

    //初始化
    function init(){
        $scope.item = {
            optionItem:'客户不在需再约时间'
        };
        $scope.imageUrls = [];
        //案例id
        $scope.id = pv6.params.caseId;
        if(!pv6.params.caseId){
            $scope.id = localStorage.getItem("pv-caseId");
        }
    }

    init();

    reloadJs();

});
//返回扫描结果
function pv2UploadImageResult(msg) {
    var $scope = getScope("pv6");
    $scope.photoResult(msg);
}